package com.lifesting.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.sun.faces.renderkit.html_basic.HtmlBasicRenderer;

public class TableRender extends HtmlBasicRenderer {
	public static final String RENDER_TYPE = "com.lifesting.render.table";

	@Override
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		super.encodeBegin(context, component);
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("table", component);
		writer.writeAttribute("id", component.getClientId(context), "id");
		writer.writeAttribute("border", "1", null);
		encodeHeader(context, component);
	}
	private void encodeHeader(FacesContext context, UIComponent component) throws IOException {
		List<UIComponent> children = component.getChildren();
		//打印表格 title
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("tr", component);
		assert(component instanceof Table);
		Table table = (Table) component;
		//如果可选，增加一个10宽度的列，并且render也会增加一个
		if (table.getRowsCheckable().booleanValue())
		{
			writer.startElement("td", component);
			writer.writeAttribute("width", 10, null);
			writer.write(" ");
			writer.endElement("td");
		}
		for(Iterator<UIComponent> ite = children.iterator(); ite.hasNext();)
		{
			UIComponent uc = ite.next();
			if (uc instanceof Column)
			{
				Column mycolumn = (Column) uc;
				printColumnHeader(mycolumn,context);
			}
		}
		writer.endElement("tr");
	}

	

	public static final String ASC_SORT_IMG = "/img/asc.gif";
	public static final String DES_SORT_IMG = "/img/des.gif";
	private void printColumnHeader(Column component, FacesContext context) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("td", component);
		writer.writeAttribute("width", component.getWidth(), "width");
		String cid = component.getClientId(context);
		writer.writeAttribute("id", cid, null);
		writer.write(component.getText());
		writer.write(" ");
		if (component.getEnableSorter())
		{
			writer.startElement("input", component);
			writer.writeAttribute("type", "image", null);
			String context_path = context.getExternalContext().getRequestContextPath();
			String context_sort_image;
			if (!"/".equals(context_path)){
				context_sort_image = context_path+(component.getSortOrder() == 0 || component.getSortOrder()==-1? ASC_SORT_IMG : DES_SORT_IMG);
			}
			else
			{
				context_sort_image = (component.getSortOrder() == 0 || component.getSortOrder()==-1? ASC_SORT_IMG : DES_SORT_IMG);
			}
			writer.writeAttribute("src", context_sort_image, null);
			String img_id = cid+".1";
			writer.writeAttribute("id",img_id,null);
			writer.writeAttribute("name", img_id, null);
			String msg = "当前:["+component.getSortOrder()+"]点击进行"+(component.getSortOrder() == Column.NON_SORT ? "正" : component.getSortOrder() == Column.ASC_SORT ? "逆" : " 正")+" 排序";
			writer.writeAttribute("title", msg, null);
			writer.endElement("input");
		}
		writer.endElement("td");
	}


	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.endElement("table");
	}

	@Override
	public void encodeChildren(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		Table table = (Table) component;
		List<UIComponent> children = component.getChildren();
		List<Column> columns = new ArrayList<Column>();
		List<UIComponent> not_column = new ArrayList<UIComponent>();
		for (Iterator<UIComponent> ite = children.iterator(); ite.hasNext();)
		{
			UIComponent uic = ite.next();
			if (uic instanceof Column){
				columns.add((Column) uic);
			}
			else
			{
				not_column.add(uic);
			}
			
		}
		String base_row_id = table.getClientId(context)+".row.";
		for (int i= 0; i < table.getReallyPageCount(); i++)
		{
			writer.startElement("tr", component);
			Record record = table.getRowRecord(i);
			if (table.getRowsCheckable().booleanValue()){
				writer.startElement("td", component);
				writer.startElement("input", component);
				writer.writeAttribute("type", "checkbox", null);
				writer.writeAttribute("id", base_row_id+record.getUniqueId(), null);
				writer.writeAttribute("name", base_row_id+record.getUniqueId(), null);
				if (record.isSelected())
					writer.writeAttribute("checked", "checked", null);
				writer.writeAttribute("value", "true", null);
				writer.endElement("input");
				writer.endElement("td");
			}
			//将var指向当前的record
			String var = table.getVar();
			
			context.getExternalContext().getRequestMap().put(var,record);
			for (int j = 0; j < columns.size(); j++)
			{
				encodeRecursive(context,columns.get(j));
			}
			writer.endElement("tr");
			context.getExternalContext().getRequestMap().remove(var);
		}
		for (int p = 0; p < not_column.size(); p++)
		{
			encodeRecursive(context, not_column.get(p));
		}
		
	}

	@Override
	public void decode(FacesContext context, UIComponent component) {
		super.decode(context, component);
		/*
		for (Iterator<String> itt= map.keySet().iterator(); itt.hasNext();)
		{
			String key = itt.next();
			System.out.println(key+" = "+map.get(key));
		}*/
	}

}
